package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.helper.HttpRequestHelper;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.common.util.MD5;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import org.springframework.data.domain.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    //删除排班信息
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request){

        //获取传递过来的医院信息，（医院信息是封装到map集合中的）
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 将Map<String, String[]>转换为Map<String, Object>
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);

        // 获取page和limit
        int page = StringUtils.isEmpty(stringObjectMap.get("page"))?1:Integer.parseInt((String) stringObjectMap.get("page"));
        int limit = StringUtils.isEmpty(stringObjectMap.get("limit"))?1:Integer.parseInt((String) stringObjectMap.get("limit"));

        // 获取医院传来的签名(签名以及经过MD5加密)
        String hospSign = (String)stringObjectMap.get("sign");

        // 根据传过来的hoscode（医院编码），查询数据库中的中的签名
        String hoscode = (String)stringObjectMap.get("hoscode");
        String hosScheduleId = (String)stringObjectMap.get("hosScheduleId");
        String singKey = hospitalSetService.getSignKey(hoscode);

        // 将查出来的签名通过MD5加密
        String singKeyMd5 = MD5.encrypt(singKey);

        //比较两个值
        if(!hospSign.equals(singKeyMd5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();

    }

    // 查询排班信息
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request){

        //获取传递过来的医院信息，（医院信息是封装到map集合中的）
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 将Map<String, String[]>转换为Map<String, Object>
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);

        // 获取page和limit
        int page = StringUtils.isEmpty(stringObjectMap.get("page"))?1:Integer.parseInt((String) stringObjectMap.get("page"));
        int limit = StringUtils.isEmpty(stringObjectMap.get("limit"))?1:Integer.parseInt((String) stringObjectMap.get("limit"));

        // 获取医院传来的签名(签名以及经过MD5加密)
        String hospSign = (String)stringObjectMap.get("sign");

        // 根据传过来的hoscode（医院编码），查询数据库中的中的签名
        String hoscode =(String)stringObjectMap.get("hoscode");
        // 科室编号
        String depcode =(String)stringObjectMap.get("depcode");
        String singKey = hospitalSetService.getSignKey(hoscode);

        // 将查出来的签名通过MD5加密
        String singKeyMd5 = MD5.encrypt(singKey);

        //比较两个值
        if(!hospSign.equals(singKeyMd5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);

        // 调用service方法
        Page<Schedule> pageModel =  scheduleService.findPageSchedule(page,limit,scheduleQueryVo);
        return Result.ok(pageModel);

    }

    // 上传排班
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){

        //获取传递过来的医院信息，（医院信息是封装到map集合中的）
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 将Map<String, String[]>转换为Map<String, Object>
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);

        // 获取医院传来的签名(签名以及经过MD5加密)
        String hospSign = (String)stringObjectMap.get("sign");

        // 根据传过来的hoscode（医院编码），查询数据库中的中的签名
        String hoscode =(String)stringObjectMap.get("hoscode");
        String singKey = hospitalSetService.getSignKey(hoscode);

        // 将查出来的签名通过MD5加密
        String singKeyMd5 = MD5.encrypt(singKey);

        //比较两个值
        if(!hospSign.equals(singKeyMd5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        // 调用service方法，实现添加
        scheduleService.save(stringObjectMap);
        return Result.ok();

    }

    //删除科室接口
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){

        //获取传递过来的医院信息，（医院信息是封装到map集合中的）
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 将Map<String, String[]>转换为Map<String, Object>
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);

        // 获取page和limit
        int page = StringUtils.isEmpty(stringObjectMap.get("page"))?1:Integer.parseInt((String) stringObjectMap.get("page"));
        int limit = StringUtils.isEmpty(stringObjectMap.get("limit"))?1:Integer.parseInt((String) stringObjectMap.get("limit"));

        // 获取医院传来的签名(签名以及经过MD5加密)
        String hospSign = (String)stringObjectMap.get("sign");

        // 根据传过来的hoscode（医院编码），查询数据库中的中的签名
        String hoscode =(String)stringObjectMap.get("hoscode");
        String depcode =(String)stringObjectMap.get("depcode");
        String singKey = hospitalSetService.getSignKey(hoscode);

        // 将查出来的签名通过MD5加密
        String singKeyMd5 = MD5.encrypt(singKey);

        //比较两个值
        if(!hospSign.equals(singKeyMd5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        departmentService.remove(hoscode,depcode);
        return Result.ok();

    }

    //查询科室信息
    @PostMapping("department/list")
     public Result findDepartment(HttpServletRequest request){

        //获取传递过来的医院信息，（医院信息是封装到map集合中的）
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 将Map<String, String[]>转换为Map<String, Object>
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);

        // 获取page和limit
        int page = StringUtils.isEmpty(stringObjectMap.get("page"))?1:Integer.parseInt((String) stringObjectMap.get("page"));
        int limit = StringUtils.isEmpty(stringObjectMap.get("limit"))?1:Integer.parseInt((String) stringObjectMap.get("limit"));

        // 获取医院传来的签名(签名以及经过MD5加密)
        String hospSign = (String)stringObjectMap.get("sign");

        // 根据传过来的hoscode（医院编码），查询数据库中的中的签名
        String hoscode =(String)stringObjectMap.get("hoscode");
        String singKey = hospitalSetService.getSignKey(hoscode);

        // 将查出来的签名通过MD5加密
        String singKeyMd5 = MD5.encrypt(singKey);

        //比较两个值
        if(!hospSign.equals(singKeyMd5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);

        // 调用service方法
        Page<Department> pageModel = (Page<Department>) departmentService.findPageDepartment(page,limit,departmentQueryVo);
        return Result.ok(pageModel);

    }


    //上传科室信息
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){

        //获取传递过来的医院信息，（医院信息是封装到map集合中的）
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 将Map<String, String[]>转换为Map<String, Object>
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);

        // 获取医院传来的签名(签名以及经过MD5加密)
        String hospSign = (String)stringObjectMap.get("sign");

        // 根据传过来的hoscode（医院编码），查询数据库中的中的签名
        String hoscode =(String)stringObjectMap.get("hoscode");
        String singKey = hospitalSetService.getSignKey(hoscode);

        // 将查出来的签名通过MD5加密
        String singKeyMd5 = MD5.encrypt(singKey);

        //比较两个值
        if(!hospSign.equals(singKeyMd5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        // 调用service方法，实现添加
        departmentService.save(stringObjectMap);
        return Result.ok();

    }

    //查询医院信息
    @PostMapping ("hospital/show")
    public Result getHospital(HttpServletRequest request){

        //获取传递过来的医院信息，（医院信息是封装到map集合中的）
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 将Map<String, String[]>转换为Map<String, Object>
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);
        //取出hoscod
         String hoscode = (String) stringObjectMap.get("hoscode");

        // 获取医院传来的签名(签名以及经过MD5加密)
        String hospSign = (String)stringObjectMap.get("sign");

        // 根据传过来的hoscode（医院编码），查询数据库中的中的签名
        String singKey = hospitalSetService.getSignKey(hoscode);

        // 将查出来的签名通过MD5加密
        String singKeyMd5 = MD5.encrypt(singKey);

        //比较两个值
        if(!hospSign.equals(singKeyMd5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        // 通过hoscode查询对象
        Hospital hospital = hospitalService.getByHoscoe(hoscode);
        return Result.ok(hospital);
    }

    //上传医院接口
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest resquest){

        //获取传递过来的医院信息，（医院信息是封装到map集合中的）
        Map<String, String[]> parameterMap = resquest.getParameterMap();
        // 将Map<String, String[]>转换为Map<String, Object>
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);

        // 获取医院传来的签名(签名以及经过MD5加密)
         String hospSign = (String)stringObjectMap.get("sign");

        // 根据传过来的hoscode（医院编码），查询数据库中的中的签名
        String hoscode =(String)stringObjectMap.get("hoscode");
        String singKey = hospitalSetService.getSignKey(hoscode);

        // 将查出来的签名通过MD5加密
        String singKeyMd5 = MD5.encrypt(singKey);

        //比较两个值
        if(!hospSign.equals(singKeyMd5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        // 传输过程中"+"变成" ",因此需要转换回来
         String logoData =(String) stringObjectMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        stringObjectMap.put("logoData",logoData);


        hospitalService.save(stringObjectMap);
        return Result.ok();
    }




}
